package com.goldplusgold.tradedelay.app.my.activity;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.goldplusgold.network.lib.AppCollectedInfoUtil;
import com.goldplusgold.network.lib.exception.ServerException;
import com.goldplusgold.support.lib.base.activity.BaseActivity;
import com.goldplusgold.support.lib.utils.DeviceNetworkUtil;
import com.goldplusgold.support.lib.widget.AsyncTask;
import com.goldplusgold.tradedelay.app.R;
import com.goldplusgold.tradedelay.app.my.networkservice.MyService;
import com.goldplusgold.tradedelay.app.my.account.UserInfo;
import com.goldplusgold.tradedelay.app.base.utils.ActivityUtils;
import com.goldplusgold.support.lib.utils.AppInfoUtils;
import com.goldplusgold.support.lib.utils.NetWorkConstant;
import com.goldplusgold.support.lib.widget.TitleView;

import butterknife.BindView;
import butterknife.OnClick;


public class WechatRegisterActivity extends BaseActivity implements Handler.Callback {

    @BindView(R.id.title_view)
    TitleView titleView;
    @BindView(R.id.et_login_pwd)
    EditText etLoginPwd;
    @BindView(R.id.btn_complete)
    Button btnComplete;
    private String mPhone;
    private String mWechatCode;
    private String mPassword;
    private Handler handler = new Handler(this);

   /* public static WechatRegisterFragment newInstance(String phone, String wechatCode) {
        WechatRegisterFragment fragment = new WechatRegisterFragment();
        Bundle args = new Bundle();
        args.putString("phone", phone);
        args.putString("wechatCode", wechatCode);
        fragment.setArguments(args);
        return fragment;
    }*/

    /*@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mPhone = getArguments().getString("phone");
            mWechatCode = getArguments().getString("wechatCode");
        }
    }
*/
    @Override
    protected void initView() {
        super.initView();
        titleView.getTitletextView().setVisibility(View.VISIBLE);
        titleView.getTitletextView().setText("手机验证");
        titleView.getBackView().setVisibility(View.VISIBLE);
        if (getIntent() != null) {
            mPhone = getIntent().getStringExtra("phone");
            mWechatCode = getIntent().getStringExtra("wechatCode");
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_wechat_register;
    }


    @OnClick({R.id.iv_back, R.id.btn_complete})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_complete:
                mPassword = etLoginPwd.getText().toString().trim();
                if (TextUtils.isEmpty(mPassword)) {
                    showToastMessage("密码不能为空!", Toast.LENGTH_SHORT);
                    return;
                }
                String imei = DeviceNetworkUtil.getIMEIWrap(WechatRegisterActivity.this);
                new WechatRegisterAsyncTask(imei).execute(AsyncTask.TYPE_NET);
                break;
        }
    }

    public class WechatRegisterAsyncTask extends AsyncTask<Void, Void, UserInfo> {
        private String imei;
        public WechatRegisterAsyncTask(String imei) {
            this.imei = imei;
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            showLoadingControl();
        }

        @Override
        protected UserInfo doInBackground(Void... params) {
            try {

                UserInfo userInfo = MyService.getInstance().wechatRegister("android", mWechatCode, "android", mPhone, mPassword, imei);
                if (userInfo != null) {
                    return userInfo;
                }
            } catch (ServerException e) {

                sendNetworkErrorMessage(NetWorkConstant.NETWORK_SERVER_ERROR_CODE, e.getMessage());

            } catch (Exception e) {
                e.printStackTrace();
                sendNetworkErrorMessage(NetWorkConstant.NETWORK_ERROR_CODE, null);
            }

            return null;
        }

        @Override
        protected void onPostExecute(UserInfo userInfo) {
            super.onPostExecute(userInfo);
            dismisLoadingControl();
            if (userInfo != null) {
                if (userInfo.getStatus()) {
                    String token = userInfo.getToken();
                    String time = String.valueOf(System.currentTimeMillis());
                    String versionName = AppInfoUtils.getVersionName(WechatRegisterActivity.this);
                    AppCollectedInfoUtil.initAppCollectedInfo(WechatRegisterActivity.this, token, time, versionName);
                    ActivityUtils.skipToTDMain(WechatRegisterActivity.this, "wechatRegister");
                }
            }
        }
    }

    @Override
    public boolean handleMessage(Message msg) {

        int type = msg.what;
        switch (type) {

            case NetWorkConstant.NETWORK_ERROR_CODE:
                showToastMessage(R.string.network_error, Toast.LENGTH_LONG);
                break;
            case NetWorkConstant.NETWORK_SERVER_ERROR_CODE:
                String errorMessage = (String) msg.obj;
                showToastMessage(errorMessage, Toast.LENGTH_LONG);
                break;
        }

        return true;
    }

    private void sendNetworkErrorMessage(int type, String errorMessage) {
        Message message = handler.obtainMessage();
        message.what = type;
        if (!TextUtils.isEmpty(errorMessage)) {
            message.obj = errorMessage;
        }
        handler.sendMessage(message);
    }
}
